package com.andy.wang.service.impl;

import com.andy.wang.pojo.Room;
import com.andy.wang.pojo.User;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.andy.wang.service.RoomService;
import com.andy.wang.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 王更元
 * @description 针对表【room(聊天房)】的数据库操作Service实现
 * @createDate 2024-04-12 09:26:35
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
        implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    private List<Room> accrooms = null;
    private ArrayList<Integer> uIds = null;


    public List<Room> selectUhobbys(String hobbys) {
        return roomMapper.selectUhobbys(hobbys);
    }

    @Override
    public List<Room> getIntelligenceRoom(String hobbys) {
        return roomMapper.getIntelligenceRoom(hobbys.split(","));
    }

    public List<Room> getRNameAndUName(String hobbys) {
        return roomMapper.getRNameAndUName(hobbys);
    }

    public List<Room> getRooms(int count, String hobbys, String search) {
        accrooms = new ArrayList<>();  // 全部数据
        List<Room> lazyAccrooms = null;  // 最终返回
        uIds = new ArrayList<>();

        List<Room> intelligenceAccroom = null;  // 分散查询
        List<Room> rNameAndUName = null;      // 姓名和房间名
        List<Room> selectUhobbys = null;    // 完全匹配查询

        accrooms.clear();
        uIds.clear();


        if (StringUtils.isBlank(search)) {
            selectUhobbys = this.selectUhobbys(hobbys);
            if (selectUhobbys.size() > 0) {
                if (uIds.size() == 0) {
                    for (int i = 0; i < selectUhobbys.size(); i++) {
                        accrooms.add(selectUhobbys.get(i));
                        uIds.add(selectUhobbys.get(i).getRid());
                    }
                } else {
                    for (int i = 0; i < selectUhobbys.size(); i++) {
                        for (int index = 0; index < uIds.size(); index++) {
                            if (selectUhobbys.get(i).getRid() != uIds.get(index)) {
                                uIds.add(selectUhobbys.get(i).getRid());
                                accrooms.add(selectUhobbys.get(i));
                            }
                        }
                    }
                }
            }

            intelligenceAccroom = this.getIntelligenceRoom(hobbys);
            if (intelligenceAccroom.size() > 0) {
                if (uIds.size() == 0) {
                    for (int i = 0; i < intelligenceAccroom.size(); i++) {
                        accrooms.add(intelligenceAccroom.get(i));
                        uIds.add(intelligenceAccroom.get(i).getRid());
                        System.out.println(uIds.get(i));
                    }
                } else {
                    System.out.println("------------------------------");
                    for (int i = 0; i < intelligenceAccroom.size(); i++) {
                        for (int index = 0; index < uIds.size(); index++) {
                            if (intelligenceAccroom.get(i).getRid() != uIds.get(index)) {
                                uIds.add(intelligenceAccroom.get(i).getRid());
                                accrooms.add(intelligenceAccroom.get(i));
                            }
                        }
                    }
                }
            }

            List<Room> roomList = roomMapper.getRooms(uIds);
            for (int i = 0; i < roomList.size(); i++) {
                uIds.add(roomList.get(i).getRid());
                accrooms.add(roomList.get(i));
            }
        }
        // 查询条件不为空时
        if (StringUtils.isNotBlank(search)) {
            hobbys = search;
            rNameAndUName = this.getRNameAndUName(hobbys);
            if (rNameAndUName.size() > 0) {
                if (uIds.size() == 0) {
                    for (int i = 0; i < rNameAndUName.size(); i++) {
                        accrooms.add(rNameAndUName.get(i));
                        uIds.add(rNameAndUName.get(i).getRid());
                    }
                } else {
                    for (int i = 0; i < rNameAndUName.size(); i++) {
                        for (int index = 0; index < uIds.size(); index++) {
                            if (rNameAndUName.get(i).getRid() != uIds.get(index)) {
                                uIds.add(rNameAndUName.get(i).getRid());
                                accrooms.add(rNameAndUName.get(i));
                            }
                        }
                    }
                }
            }

            intelligenceAccroom = this.getIntelligenceRoom(hobbys);
            if (intelligenceAccroom.size() > 0) {
                if (uIds.size() == 0) {
                    for (int i = 0; i < intelligenceAccroom.size(); i++) {
                        accrooms.add(intelligenceAccroom.get(i));
                        uIds.add(intelligenceAccroom.get(i).getRid());
                    }
                } else {
                    for (int i = 0; i < intelligenceAccroom.size(); i++) {
                        for (int index = 0; index < uIds.size(); index++) {
                            if (intelligenceAccroom.get(i).getRid() != uIds.get(index)) {
                                uIds.add(intelligenceAccroom.get(i).getRid());
                                accrooms.add(intelligenceAccroom.get(i));
                            }
                        }
                    }
                }
            }
        }
        if (count > 0) {
            lazyAccrooms = new ArrayList<>();
            for (int i = 0; i < accrooms.size(); i++) {
                if (i < count) {
                    lazyAccrooms.add(accrooms.get(i));
                } else {
                    break;
                }
            }
        }
        return lazyAccrooms;
    }

    @Override
    public int countRooms() {
        return accrooms.size();
    }

    @Override
    public List<User> getRoomUsers(int rID) {
        return roomMapper.getRoomUsers(rID);
    }

    @Override
    public int last() {
        return roomMapper.last();
    }

    @Override
    public Room AllShuJu(Integer rID) {
        return roomMapper.AllShuJu(rID);
    }
}




